package com.bdqn.dao.Impl;

import com.bdqn.dao.BaseDao;
import com.bdqn.dao.NewsDao;
import com.bdqn.entity.News;
import com.bdqn.util.PageSupport;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <h3>项目名称:demo02222</h3>
 * <p>本API描述:新闻操作的实现类</p>
 * <p>@author : AliyZhang</p>
 * <p>@date : 2021-02-23 14:11</p>
 **/
//继承baseDao中的方法，同时实现newsDao的方法
public class NewsDaoImpl extends BaseDao implements NewsDao {


    @Override
    public int addNews(News news) {
        //SQL字符串
        String sql="insert into news_detail(categoryId,title,summary,content,picPath,author,createDate) values(?,?,?,?,?,?,?)";
        //参数数组
        Object[] params=new Object[7];
        params[0]=news.getCategoryId();
        params[1]=news.getTitle();
        params[2]=news.getSummary();
        params[3]=news.getContent();
        params[4]=news.getPicPath();
        params[5]=news.getAuthor();
        params[6]=news.getCreateDate();
        //调用从父类baseDao继承的通用增删改方法
        int rows=executeUpdate(sql,params);
        return rows;
    }

    @Override
    public int deleteNewsById(int id) {
        //SQL字符串
        String sql="delete from news_detail where id=?";
        //参数数组
        Object[] params=new Object[1];
        params[0]=id;
        int rows=executeUpdate(sql,params);
        return rows;
    }

    @Override
    public int updateNews(News news) {
        //SQL字符串
        String sql="update news_detail set categoryId=?,title=?," +
                "summary=?,content=?,picPath=?,author=?,modifyDate=? where id=?";
        //参数数组
        Object[] params=new Object[8];
        params[0]=news.getCategoryId();
        params[1]=news.getTitle();
        params[2]=news.getSummary();
        params[3]=news.getContent();
        params[4]=news.getPicPath();
        params[5]=news.getAuthor();
        params[6]=news.getModifyDate();
        params[7]=news.getId();
        int rows=executeUpdate(sql,params);
        return rows;
    }

    @Override
    public List<News> queryNewsAll() {
        //SQL字符串
        String sql="select * from news_detail";
        //执行查询语句，返回结果集
        ResultSet rs=executeQuery(sql,null);
        //创建list集合，封装数据
        List<News> list=new ArrayList<>();
        //循环遍历结果集
        try {
            while(rs.next()){
                int id=rs.getInt("Id");
                int categoryId=rs.getInt("categoryId");
                String title=rs.getString("title");
                String summary=rs.getString("summary");
                String content=rs.getString("content");
                String picPath = rs.getString("picPath");
                String author = rs.getString("author");
                Date createDate = rs.getTimestamp("createDate");
                Date modifyDate = rs.getTimestamp("modifyDate");
                //把结果集中的每一条记录转换为一个新闻对象
                News news = new News(id, categoryId, title, summary, content, picPath, author, createDate, modifyDate);
                list.add(news);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public News queryNewsById(int id) {
        String sql="select * from news_detail where id=?";
        Object[] params=new Object[1];
        params[0]=id;
        //调用通用的查询方
        ResultSet rs = executeQuery(sql, params);
        News news=null;
        try {
            while(rs.next()) {
                int categoryId = rs.getInt("categoryId");
                String title = rs.getString("title");
                String summary = rs.getString("summary");
                String content = rs.getString("content");
                String picPath = rs.getString("picPath");
                String author = rs.getString("author");
                Date createDate = rs.getTimestamp("createDate");
                Date modifyDate = rs.getTimestamp("modifyDate");
                //把结果集中的每一条记录转换为一个新闻对象
                news = new News(id, categoryId, title, summary, content, picPath, author, createDate,modifyDate);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return news;
    }

    @Override
    public int queryNewsTotal() {
        String sql="select count(id) from news_detail";
        //调用通用的查询方法
        ResultSet rs = executeQuery(sql, null);
        int total=0;
        try {
            if(rs.next()){
                total=rs.getInt(1);//将数据读取出来，获取总数据
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return total;
    }

    @Override
    public void queryPageNews(PageSupport<News> page) {
        /*
        page：①当前页码，②每页数据容量，③每页数据集合，④数据总条数信息，⑤总页数信息
        ?,?
        分页查询的起始位置（当前页码-1）*pageSize
        每页的最大条数
         */
        //SQL字符串
        String sql="select * from news_detail order by id limit ?,?";
        //参数数组
        Object[] params=new Object[2];
        params[0]=(page.getCurPageNo()-1)*page.getPageSize();
        params[1]=page.getPageSize();
        //调用通用的查询方法
        ResultSet rs = executeQuery(sql, params);
        //声明集合存储查询的数据
        List<News> list=new ArrayList<>();
        try {
            while(rs.next()){
                int id=rs.getInt("Id");
                int categoryId=rs.getInt("categoryId");
                String title=rs.getString("title");
                String summary=rs.getString("summary");
                String content=rs.getString("content");
                String picPath = rs.getString("picPath");
                String author = rs.getString("author");
                Date createDate = rs.getTimestamp("createDate");
                Date modifyDate = rs.getTimestamp("modifyDate");
                //把结果集中的每一条记录转换为一个新闻对象
                News news = new News(id, categoryId, title, summary, content, picPath, author, createDate, modifyDate);
                list.add(news);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        //把查询出来的本页数据存储到传进来的page对象中
        page.setDatas(list);
        //在page里设置总条数
        page.setTotal(queryNewsTotal());
    }

    public static void main(String[] args) {
        //1.添加数据
        NewsDaoImpl newsDao = new NewsDaoImpl();
        /*try {
            News news = new News(2, "你好，李焕英", "你好，李焕英",
                    "听说很感人", "",
                    "贾玲", new SimpleDateFormat("yyyy-MM-dd").parse("2021-02-12"));
            int rows = newsDao.addNews(news);
            if(rows>0){
                System.out.println("新闻添加成功！");
            }else{
                System.out.println("新闻添加失败！");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }*/

        //2.查询数据
        /*List<News> list=newsDao.queryNewsAll();
        System.out.println(list+"\t");*/

        //3.删除数据
        /*int rows = newsDao.deleteNewsById(12);
        if(rows>0){
            System.out.println("新闻删除成功！");
        }else{
            System.out.println("新闻删除失败！");
        }*/
        //4.修改数据

        try {
            News news = new News(13, 3, "唐人街探案3", "唐探3", "......", "", "陈思诚", new SimpleDateFormat("yyyy-MM-dd").parse("2021-02-12"));
            int rows = newsDao.updateNews(news);
            if(rows>0){
                System.out.println("新闻修改成功！");
            }else{
                System.out.println("新闻修改失败！");
            }
        } catch (ParseException e) {
            e.printStackTrace();

        }



    }
}
